package pl.cezary.butler.walili.common;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

/**
 *  <p style="margin-top: 0">
 *    Obiekt zawierajacy dane logowania. Sluzacy do identyfikacji podczas 
 *    procesu logowania oraz rejestracji.
 *      </p>
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.A818EE6B-B842-1FF8-C461-6CE6A205A904]
// </editor-fold> 
public class User implements Serializable {
    
    static final long serialVersionUID = -7687733493441457328L;
    
    private static MessageDigest md5;
    static{
        try{
        md5 =MessageDigest.getInstance("MD5");
        }
        catch(NoSuchAlgorithmException e){
            
        }
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0DF1DCF8-B38A-1134-68FA-0F87D9814BD4]
    // </editor-fold> 
    private String login;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2A869813-C2E7-8629-C84F-056CE256B926]
    // </editor-fold> 
    private String haslo_sha;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.FFE04651-F76A-9E4F-A92E-8A443DDD6402]
    // </editor-fold> 
    public User() {        
    }

    public User(String login, String haslo) {
        this();
        setLogin(login);
        setHaslo(haslo);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.36009ACE-8ACB-EB4B-0097-CFF48250008D]
    // </editor-fold> 
    /**
     * Metoda pozwalajaca na odebranie hasla w postaci zaszyfrowanej algorytmem jednostronnym
     * @return zaszyfrowane haslo
     */
    public String getHaslo() {
        return haslo_sha;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.0A2A6BA4-2D9C-89EF-5A02-F843340DED6E]
    // </editor-fold> 
    /**
     * Metoda pozwalajaca na zapisanie hasla w obiekcie w postaci zaszyfrowanej algorytmem jednostronnym
     * @param haslo niezaszyfrowane haslo
     */
    public void setHaslo(String haslo) {
        byte[] md5array;
        synchronized(md5){        
            md5array = md5.digest(haslo.getBytes());
        }
        final Formatter formatter = new Formatter();
        for (final byte digestByte : md5array) {
            formatter.format("%02x", digestByte);
        }
        this.haslo_sha = formatter.toString();
        //assert(md5array.length == this.haslo_sha.length());
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.951E2B49-570F-E9EE-4DB9-6D70FC5BAFB9]
    // </editor-fold> 
    /**
     * Metoda umozliwiajaca pobranie loginu
     * @return login
     */
    public String getLogin() {
        return login;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.7C38C490-22D9-AAA6-0948-288967EA5EB9]
    // </editor-fold> 
    /**
     * Metoda pozwalajaca na ustawienie loginu
     * @param login
     */
    public void setLogin(String login) {
        this.login = login;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.login == null || !this.login.equals(other.login)) {
            return false;
        }
        if (this.haslo_sha == null || !this.haslo_sha.equals(other.haslo_sha)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.login != null ? this.login.hashCode() : 0);
        hash = 97 * hash + (this.haslo_sha != null ? this.haslo_sha.hashCode() : 0);
        return hash;
    }
    
}

